package com.javatech.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * the AiController
 *
 * @author java-tech
 * @date 2025-02-26
 */
@Slf4j
@RestController
public class AiController {

    @Autowired(required = false)
    private OllamaChatModel ollamaChatModel;

    @Autowired(required = false)
    private ChatClient.Builder chatBuilder;


    @GetMapping("chat")
    public String chat(@RequestParam("message") String message) {
        String resp = ollamaChatModel.call(message);
        log.info("*** req={}, resp={}", message, resp);
        return resp;
    }

    @GetMapping(value = "chat2") //POST
    public ResponseEntity<Flux<String>> chat2(@RequestParam("message") String message) {
        log.info("---> req={}", message);
        ChatClient client = chatBuilder.build();
        Flux<String> content = client.prompt(message).stream().content();
        log.info("resp={}", content);
        return ResponseEntity.ok(content);
    }


}
